/**
 * File: ReadDataReader.java
 * Created by: mhaimel
 * Created on: 18 Aug 2009
 * CVS:  $Id: ReadDataReader.java,v 1.1 2009/08/27 14:52:44 mhaimel Exp $
 */
package uk.ac.ebi.curtain.util.io;

import java.io.BufferedReader;
import java.io.File;
import java.util.Iterator;

import uk.ac.ebi.velvet.model.ReadData;
import uk.ac.ebi.velvet.model.Strand;
import uk.ac.ebi.velvet.model.impl.ReadDataImpl;

/**
 * @author mhaimel
 *
 */
public class ReadDataReader extends AbstractReader<ReadData> {

	public ReadDataReader(File in) {
		super(in);
	}
	
	public static void main(String[] args) {
		// 181
		for(ReadData rd : new ReadDataReader(new File("/homes/mhaimel/resources/velvetProd/output/human_cortext/supernodes_4_31/data_10/curtain_1/data_read.idx.gz"))){
			System.out.println(rd);
			System.out.println(rd.getFilePosition(Strand.FWD)+"\t"+rd.getFilePosition(Strand.REV)+"\t"+rd.getFilePosition(Strand.UNKOWN)+"\t"+rd.getFilePosition());
			if(rd.getIdentifier() == 181){
				System.out.println(rd);
			}
		}		
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Iterator<ReadData> _buildIter(File inFile) {
		return new ReadDataIter(inFile);
	}
	
	public static class ReadDataIter extends AbstractIter<ReadData>{

		public ReadDataIter(File f, BufferedReader in) {
			super(f, in);
		}
		public ReadDataIter(File f) {
			super(f);
		}

		/* ID | length | FilePos FWD | FilePos REV | FilePos UNKNOWN |    */
		@Override
		protected ReadData _buildEntry(String[] arr) {
			ReadDataImpl rd = new ReadDataImpl(Integer.valueOf(arr[0]).intValue());
			rd.setLength(Integer.valueOf(arr[1]).intValue());
			setFilePosition(rd,Strand.FWD,arr[2]);
			setFilePosition(rd,Strand.REV,arr[3]);
			setFilePosition(rd,Strand.UNKOWN,arr[4]);
			return rd;
		}
		
		private void setFilePosition(ReadDataImpl rd, Strand strand, String val) {
			long pos = Long.valueOf(val).longValue();
			if(pos >= 0){
				rd.setFilePosition(strand, pos);
			}
		}
		
	}
	
}
